//
//  BVTabBarController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/9/28.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVTabBarController.h"
#import "BVIndexViewController.h"
#import "BVCategoryViewController.h"
#import "BVShoppingCartViewController.h"
#import "BVMineViewController.h"
#import "UIImage+BVImage.h"
#import "BVNavigationController.h"

@interface BVTabBarController ()<UITabBarDelegate>
@property (nonatomic, assign) NSInteger index;
@end

@implementation BVTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupAllChildViewControllers];
}

- (void)setupAllChildViewControllers{
    
    BVIndexViewController *index = [[BVIndexViewController alloc] init];
    [self setupChildViewController:index image:[UIImage imageNamed:@"shouye"] selectImage:[UIImage imageWithOriginalName:@"hongshouye"] title:@"首页"];

    BVCategoryViewController *category = [[BVCategoryViewController alloc] init];
    [self setupChildViewController:category image:[UIImage imageNamed:@"fenli"] selectImage:[UIImage imageWithOriginalName:@"hongfenlei"] title:@"分类"];
    
    BVShoppingCartViewController *shoppingCart = [[BVShoppingCartViewController alloc] init];
    [self setupChildViewController:shoppingCart image:[UIImage imageNamed:@"gouwuche"] selectImage:[UIImage imageWithOriginalName:@"honggouwuche"] title:@"购物车"];
    
    BVMineViewController *mine = [[BVMineViewController alloc] init];
    [self setupChildViewController:mine image:[UIImage imageNamed:@"huisewode"] selectImage:[UIImage imageWithOriginalName:@"hongwode"] title:@"我的"];
}

/**
 *  添加子控制器
 *
 *  @param viewController 子控制器
 *  @param image          图片
 *  @param selectImage    选中图片
 *  @param title          item的标题
 */
- (void)setupChildViewController:(UIViewController *)viewController image:(UIImage *)image selectImage:(UIImage *)selectImage title:(NSString *)title{
    
    viewController.title = title;
    viewController.tabBarItem.image = image;
    viewController.tabBarItem.selectedImage = selectImage;
    
    BVNavigationController *nav = [[BVNavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:nav];
}

/**
 *  设置TabbarItem的主题
 */
+ (void)initialize{
    
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attr[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    NSMutableDictionary *selectedAttr = [NSMutableDictionary dictionary];
    selectedAttr[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectedAttr[NSForegroundColorAttributeName] = [UIColor BVBaseOrangeColor];
    
    UITabBarItem *items = [UITabBarItem appearance];
//    [items setTitlePositionAdjustment:UIOffsetMake(0, -1.)];
    [items setTitleTextAttributes:attr forState:UIControlStateNormal];
    [items setTitleTextAttributes:selectedAttr forState:UIControlStateSelected];
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if ([item.title isEqualToString:@"首页"]) {
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
